// app/api/download/route.ts
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';

// 文件下载接口
export async function GET(req: Request, { params }: { params: { slug: string[] } }) {
    const slug = params.slug || [];
    const fileName = slug.slice(-1); // 文件名称
    const filePath = path.join(process.cwd(), process.env.FILE_LOCATION || '/', slug.join('/'));

    if (!fs.existsSync(filePath)) {
        return NextResponse.json({ error: 'File not found' }, { status: 404 });
    }

    const fileBuffer = fs.readFileSync(filePath);
    return new Response(fileBuffer, {
        headers: {
            'Content-Type': 'application/octet-stream',
            'Content-Disposition': `attachment; filename=${fileName}`,
        },
    });
}
